local sk__tongxin = fk.CreateSkill {

  name = "sk__tongxin",

  tags = { Skill.Limited, },

}



sk__tongxin:addEffect(fk.AskForPeaches, {
  name = "sk__tongxin",
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__tongxin.name) and player.dying and player:usedSkillTimes(sk__tongxin.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if player:isWounded() then
      room:recover({
        who = player,
        num = math.min(2, player.maxHp) - player.hp,
        recoverBy = player,
        skillName = sk__tongxin.name,
      })
    end
    local generals = {}
    for _, str in ipairs({"guansuo", "baosanniang"}) do
      if not table.find(player.room.players, function(p)
        return string.find(p.general, str)~=nil or string.find(p.deputyGeneral, str)~=nil
      end) then
        for name, general in pairs(Fk.generals) do
          if general.package.extensionName ~= "hegemony" and string.find(name, str) then
            table.insert(generals, name)
          end
        end
      end
    end
    if #generals == 0 then return end
    local result = room:askForCustomDialog(player, sk__tongxin.name, "packages/utility/qml/ChooseGeneralsAndChoiceBox.qml", {
      generals,
      {"OK"},
      "#sk__tongxin-choose",
      {"Cancel"},
    })
    local general = ""
    if result == "" then
      return
    else
      local reply = result
      if reply.choice == "OK" then
        general = reply.cards[1]
      else
        return
      end
    end
    table.removeOne(room.general_pile, general)
    local isDeputy, kingdomChange = false, "shu"
    if player.deputyGeneral ~= nil and (player.deputyGeneral == "sk__guansuo" or player.deputyGeneral == "baosanniang") then
      isDeputy = true
      kingdomChange = player.kingdom
    end
    room:changeHero(player, general, false, isDeputy, true, false, kingdomChange)
  end,
})

return sk__tongxin